<template>
  <el-dialog
    :title="!dataForm.id ? '添加好友' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
    <el-form-item>
        <el-input v-model="dataForm.mobile" placeholder="电话号码" clearable></el-input>
        
      </el-form-item>
      <el-form-item>
        
        <el-button @click="getDataListByMobile()">用户查询</el-button>
        
      </el-form-item>
      </el-form>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
    style="width: 100%;">
    <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        v-if="false"
        label="id">
      </el-table-column>
    <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        label="电话号码">
      </el-table-column>
    <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          
          <el-button type="text" size="small" @click="addHandle(scope.row.userId)">添加</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-form v-if="show" :model="dataFormAdd" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="验证消息" prop="friMsg" style="margin-top: 30px">
      <el-input type="textarea" v-model="dataFormAdd.friMsg" placeholder="验证消息"></el-input>
    </el-form-item>
     <!-- <el-form-item label="rid" prop="rid"> -->
      <el-input v-if="false"  v-model="dataForm.rid" placeholder="接收人ID"></el-input>
    <!-- </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px"> -->
    <!-- <el-form-item label="发起人ID" prop="friSender">
      <el-input v-model="dataForm.friSender" placeholder="发起人ID"></el-input>
    </el-form-item>
    <el-form-item label="接收人ID" prop="friReceiver">
      <el-input v-model="dataForm.friReceiver" placeholder="接收人ID"></el-input>
    </el-form-item>
    <el-form-item label="关系状态 1 验证中 2 验证通过 3 申请拒绝" prop="friState">
      <el-input v-model="dataForm.friState" placeholder="关系状态 1 验证中 2 验证通过 3 申请拒绝"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="好友电话" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="电话号码"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="验证消息" prop="friMsg">
      <el-input v-model="dataForm.friMsg" placeholder="验证消息"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="操作时间" prop="friTime">
      <el-input v-model="dataForm.friTime" placeholder="操作时间"></el-input>
    </el-form-item>
    <el-form-item label="备注1" prop="friBz1">
      <el-input v-model="dataForm.friBz1" placeholder="备注1"></el-input>
    </el-form-item>
    <el-form-item label="备注2" prop="friBz2">
      <el-input v-model="dataForm.friBz2" placeholder="备注2"></el-input>
    </el-form-item> -->
    <!-- </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span> -->
  </el-dialog>
</template>

<script>
  export default {
    // activated(){
    //     this.dataForm.mobile='',
    //     this.getDataListByMobile()
    // },
    data () {
      return {
        visible: false,
        dataListLoading: false,
        show:false,
        dataList: [],
        dataForm: {
          friId: 0,
          // friSender: '',
          // friReceiver: '',
          // friState: '',
          userId:'',
          mobile:'',
          // friMsg: '',
          // friTime: '',
          // friBz1: '',
          // friBz2: ''
        },
        dataFormAdd:{
          rid:'',
          friMsg:'',
        },
        dataRule: {
          // friSender: [
          //   { required: true, message: '发起人ID不能为空', trigger: 'blur' }
          // ],
          // friReceiver: [
          //   { required: true, message: '接收人ID不能为空', trigger: 'blur' }
          // ],
          // friState: [
          //   { required: true, message: '关系状态 1 验证中 2 验证通过 3 申请拒绝不能为空', trigger: 'blur' }
          // ],
          mobile: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          friMsg: [
            { required: true, message: '验证消息不能为空', trigger: 'blur' }
          ]
        //   friTime: [
        //     { required: true, message: '操作时间不能为空', trigger: 'blur' }
        //   ],
        //   friBz1: [
        //     { required: true, message: '备注1不能为空', trigger: 'blur' }
        //   ],
        //   friBz2: [
        //     { required: true, message: '备注2不能为空', trigger: 'blur' }
        //   ]
        }
      }
    },
    methods: {
      addHandle(rid){
        this.show=true;
        this.dataFormAdd.rid=rid;
        // console.log(this.dataFormAdd.rid)

      },
      init () {
        // this.dataForm.friId = id || 0
        this.dataFormAdd.rid=''
        this.dataFormAdd.friMsg=''
        this.dataForm.mobile=''
        this.dataList=[]
        this.show=false
        this.visible = true
        // this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields()
          // if (this.dataForm.friId) {
          //   this.$http({
          //     url: this.$http.adornUrl(`/generator/friends/info/${this.dataForm.friId}`),
          //     method: 'get',
          //     params: this.$http.adornParams()
          //   }).then(({data}) => {
          //     if (data && data.code === 0) {
                // this.dataForm.friSender = data.friends.friSender
                // this.dataForm.friReceiver = data.friends.friReceiver
                // this.dataForm.friState = data.friends.friState
                // this.dataForm.friMsg = data.friends.friMsg
                // this.dataForm.friTime = data.friends.friTime
                // this.dataForm.friBz1 = data.friends.friBz1
                // this.dataForm.friBz2 = data.friends.friBz2
          //     }
          //   })
          // }
        // })
      },
      //电话查询用户
      getDataListByMobile(){
        
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/friends/friendListByMobile'),
          method: 'get',
          params: this.$http.adornParams({
            // 'page': this.pageIndex,
            // 'limit': this.pageSize,
            'mobile': this.dataForm.mobile
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            // this.dataList = data.page.list
            // this.totalPage = data.page.totalCount
            this.dataList = data.list
          } else {
            this.dataList = []
            // this.totalPage = 0
          }
          this.dataListLoading = false
          this.show = false
        })
      },
      // 表单提交
      dataFormSubmit () {
        
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/friends/${!this.dataForm.friId ? 'add' : 'update'}`),
              method: 'post',
              params: this.$http.adornParams({
                // 'friId': this.dataForm.friId || undefined,
                // 'friSender': this.dataForm.friSender,
                // 'friReceiver': this.dataForm.friReceiver,
                // 'friState': this.dataForm.friState,
                'rid': this.dataFormAdd.rid,
                'friMsg': this.dataFormAdd.friMsg,
                
                // 'friTime': this.dataForm.friTime,
                // 'friBz1': this.dataForm.friBz1,
                // 'friBz2': this.dataForm.friBz2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>

</style>
